# @CreateTime : 2019/11/26 15:40 
# @Author : may
# @Desc : 项目列表页面
import time
from selenium.webdriver.common.by import By
from base import Base
from logger import Logger
import config


class ProjectList(Base):
    """
    项目列表
    """
    logger = Logger('ProjectList').get_log()

    def click_project_list(self):
        """点击项目列表并切换iframe"""
        self.click((By.CLASS_NAME, 'xmlb'))
        self.switch_to_xmlb()

    def switch_to_xmlb(self):
        """切换至 xmlb iframe"""
        self.switch_to_default_content()
        self.switch_to_frame('xmlb')

    def input_stage_name(self, name):
        """
        输入分期名称

        :param name: 分期名称
        :type name: str
        :return: 分期名称
        :rtype: str
        """
        locator = (By.NAME, 'Fqms')
        self.send_keys(locator, name)
        return self.get_attribute(locator, 'value')

    def input_gnote(self, is_sure=True, address='沧州市中心医院', no='116'):
        """
        输入经纬度，默认 116.84384 , 38.307786

        :param no: 经纬度
        :type no: str
        :param is_sure: 确定还是取消
        :type is_sure: bool
        :param address: 地址
        :type address: str
        :return: 经纬度
        :rtype: str
        """

        self.click((By.NAME, 'pos'))
        self.switch_to_frame('mapId')
        self.send_keys((By.ID, 'tipinput'), address)
        time.sleep(1)
        # 选择位置
        loc = (By.XPATH, "//div[@id='amap-sug0']")
        self.click(loc)

        # 等待填充经纬度
        self.text_to_be_present_in_value((By.ID, 'tipinput1'), no)
        if is_sure:
            self.click((By.CLASS_NAME, 'saveBlue'))
        else:
            self.click((By.CLASS_NAME, 'tuichu'))
        self.switch_to_default_content()
        self.switch_to_xmlb()

        return self.get_attribute((By.ID, 'pos'), 'value')

    def get_project_address(self):
        """
        获取项目地址

        :return:
        :rtype:
        """
        return self.get_attribute((By.NAME, 'Xmdz'))

    def select_project_region(self, num):
        """
        选择项目所属行政区划(输入经纬度后会自动选择，或是在输入经纬度后没有自动回填的情况下手动选择)

        :param num: 数字1-20，代表各行政区划
            1: 空
            2：新华区
            3：运河区
            4：渤海新区
            ...
        :type num: int
        :return: 行政区划
        :rtype: str
        """
        locator = (By.XPATH, "//input[@placeholder='请选择项目所属行政区划']")
        self.click(locator)
        self.click((By.XPATH, "//div[@class='layui-form-select layui-form-selected']//dd[{}]".format(num)))
        return self.get_attribute(locator)

    def select_project_town(self, num=2):
        """
        选择项目所属街道乡镇(输入经纬度后会自动选择，所以也可以不选择；或是在输入经纬度后没有自动回填的情况下手动选择)

        :param num: 1-3,代表街道乡镇
            1：空
            2：建北街道（新华区）或水月寺街道（运河区）
            3：车站街道（新华区）或南环中路街道（运河区）
        :type num: int
        :return: 街道乡镇
        :rtype: str
        """
        xpath = ''
        locator = (By.XPATH, "//input[@placeholder='请选择项目所属街道乡镇']")
        self.click(locator)
        time.sleep(1)
        if num == 1:
            xpath = "//div[@class='layui-form-select layui-form-selected']//dd[1]"
        if num == 2:
            xpath = "//div[@class='layui-form-select layui-form-selected']//dd[2]"
        if num == 3:
            xpath = "//div[@class='layui-form-select layui-form-selected']//dd[3]"
        self.click((By.XPATH, xpath))
        return self.get_attribute(locator, 'value')

    def get_town(self):
        """获取所属街道乡镇"""
        return self.get_attribute((By.XPATH, "//input[@placeholder='请选择项目所属街道乡镇']"))

    def select_tax_organ(self, town=None):
        """
        选择项目管理税务机关
        :param town: 乡镇街道
        :type town: str
        :return: 税务机关
        :rtype: str
        """
        locator = (By.ID, 'treeclass')
        self.click(locator)
        # 这里不强制等待容易选择不到县级税务机关
        time.sleep(2)
        if town == '建北街道':
            xpath = "//body//ul[@class='layui-show']//ul[@class='layui-show']//li[2]//a[1]//cite[1]"
        else:
            xpath = '//*[@id="classtree"]/li[1]/ul[1]/li[5]/ul[1]/li[1]/a[1]/cite[1]'
        self.click((By.XPATH, xpath))
        return self.get_text((By.ID, 'treeclass'))

    def select_is_cost_apportion(self, is_cost_apportion):
        """
        选择项目间是否有成本分摊比列

        :param is_cost_apportion: 有无成本分摊
        :type is_cost_apportion: str
        :return:
        :rtype:
        """

        if is_cost_apportion == '有':
            num = 1
        else:
            num = 3

        # 有无项目间成本分摊比例单选框xpath
        xpath = f'/html[1]/body[1]/div[3]/div[2]/div[1]/div[1]/dl[2]/dd[1]/form[1]/div[12]/div[1]/div[{num}]/i[1]'
        self.click((By.XPATH, xpath))

    def input_cost_apportion(self, num='55.22'):
        """
        填写成本分摊比列，前提：有成本分摊

        :param num: 0 < num < 100
        :type num: str
        :return: 成本分摊比列
        :rtype: str
        """
        locator = (By.NAME, 'cbftbl')
        self.send_keys(locator, num)
        return self.get_attribute(locator, 'value')

    def select_is_land_apportion(self, is_land_apportion):
        """
        选择项目间有无土地分摊

        :param is_land_apportion: 有无土地分摊
        :type is_land_apportion: str
        """

        if is_land_apportion == '有':
            num = 1
        else:
            num = 3

        xpath = f'/html[1]/body[1]/div[3]/div[2]/div[1]/div[1]/dl[2]/dd[1]/form[1]/div[12]/div[2]/div[{num}]/i[1]'
        self.click((By.XPATH, xpath))

    def input_land_apportion(self, proportion='66.66'):
        """
        输入土地分摊比列，前提：有土地分摊

        :param proportion: 分摊比例
        :type proportion: str
        :return: 分摊比例
        :rtype: str
        """
        locator = (By.NAME, 'tdftbl')
        self.send_keys(locator, proportion)
        return self.get_attribute(locator, 'value')

    def save_stage(self, name, stage):
        """保存-新建分期"""

        # 保存按钮
        self.click((By.XPATH, "//div[@id='slztid']//a[@class='saveBlue']"))
        # 二次确认：
        #   该项目下其他分期存在成本分摊比例的情况
        #   该项目下其他分期存在土地分摊比例的情况
        #   是否继续保存
        self.switch_to_xmlb()
        self.click((By.XPATH, "//a[@class='layui-layer-btn0']"))
        self.switch_to_default_content()
        text = self.get_attribute((By.XPATH, "//div[@class='layui-layer-content']"), 'textContent')

        # 关闭项目基本情况窗口
        self.x_btn_basic_info()
        xmid = self.get_xmid(name, stage)
        msg = 'fail'
        if xmid and text == '项目创建完成！':
            config.set_cfg('project', 'stage_a', stage)
            msg = 'success'
            self.logger.info('分期创建完成')
        else:
            self.logger.error('分期创建失败')
        return msg

    def click_file_upload_btn(self):
        """
        上传附件按钮，默认三期（第一个分期）
        """
        self.switch_to_xmlb()
        self.click((By.XPATH, '//tr[2]//td[10]//div[1]//a[1]//span[1]'))

    def x_btn_upload(self):
        """关闭上传附件窗口"""
        self.switch_to_default_content()
        self.click((By.XPATH, "//a[@class='layui-layer-ico layui-layer-close layui-layer-close1']"))

    def x_btn_basic_info(self):
        """关闭项目基本情况窗口"""
        self.switch_to_xmlb()
        self.click((By.XPATH, "//a[@class='layui-layer-ico layui-layer-close layui-layer-close1']"))
        # self.find_element_by_xpath('/html[1]/body[1]/div[4]/span[1]/a[1]').click()

    def switch_to_file_upload(self):
        self.switch_to_default_content()
        iframe = self.find_element((By.XPATH, '/html[1]/body[1]/div[15]/div[2]/iframe[1]'))
        self.driver.switch_to.frame(iframe)

    # def file_upload(self, file_name='upload_1pic.exe'):
    #     """
    #     上传规划总平图
    #
    #     :param file_name: 附件类型与个数
    #     :type file_name: str
    #     :return: 上传附件个数
    #     :rtype: str
    #     """
    #     self.click_file_upload_btn()
    #     self.switch_to_file_upload()
    #     res = self.upload(file_name)
    #     count = res.get('count')
    #     if count:
    #         self.logger.info('前台附件数量:' + str(count))
    #     else:
    #         return res.get('msg')
    #     file_names = self.get_file_names(ProjectList.xmid)
    #     file_names_server = self.win_file(xmid=ProjectList.xmid)
    #     msg = 'fail'
    #     # 库中的附件数量且库中的附件名称在服务器相应文件夹中
    #     if count == len(file_names) and set(file_names).issubset(file_names_server):
    #         msg = 'success'
    #     return msg

    # def delete_files(self):
    #     """
    #     删除附件
    #
    #     :return: 是否成功
    #     :rtype: str
    #     """
    #     self.switch_to_xmlb()
    #     self.click_file_upload_btn()
    #     self.switch_to_file_upload()
    #     res = self.delete_file(ProjectList.xmid)
    #     ProjectList.count = res.get('value')
    #     msg = 'fail'
    #     if res.get('msg') == 'success':
    #         msg = 'success'
    #     if res == '没有附件':
    #         msg = '没有附件'
    #     return msg

    def click_edit_btn_project(self):
        """编辑项目名称按钮"""

        tr = self.get_tr_of_current_project()
        # self.click((By.XPATH, '//tr[1]//td[11]//div[1]//a[2]//span[1]'))
        tr.find_element_by_link_text('编辑').click()

    def update_project_name(self, case, name):
        """
        修改项目名称

        :param name: 项目名称
        :type name: str
        :param case: 用例
        :type case: str
        :return:
        :rtype:
        """

        self.refresh()
        # 点击编辑按钮
        self.click_project_list()
        self.click_edit_btn_project()
        # 项目名称不为空
        if case == '正常修改':
            # 项目名称写入配置文件
            config.set_cfg('project', 'project_name', name)
            # 修改名称
            self.send_keys((By.ID, 'XM_MC'), name)
            # 保存
            self.click((By.XPATH, "//div[@id='xmEdit']//a[@class='saveBlue']"))
            config.set_cfg('project', 'project_name', name)
            # 确定
            self.click((By.XPATH, '//div[@class="layui-layer layui-layer-dialog"]//div[3]//a'))
            locator = (By.XPATH, '//div[@type="dialog"]//div[@class="layui-layer-content"]')
            is_present = self.text_to_be_present_in_text(locator, '修改成功!')
            msg = 'failure'
            if is_present:
                self.logger.info(f'项目名称修改完毕，项目名称：{name}')
                msg = 'success'

            return msg
        # 项目名为空
        if case == '项目名称为空':
            self.find_element((By.ID, 'XM_MC')).clear()
            # 保存
            self.click((By.XPATH, "//div[@id='xmEdit']//a[@class='saveBlue']"))
            msg = self.get_attribute((By.XPATH, '//div[@type="dialog"]//div[@class="layui-layer-content"]'),
                                     'textContent')
            # 关闭窗口
            self.x_edit_project_name()
            time.sleep(1)
            return msg
        # 不修改项目名称直接保存
        self.click((By.XPATH, "//div[@id='xmEdit']//a[@class='saveBlue']"))
        msg = self.get_attribute((By.XPATH, '//div[@type="dialog"]//div[@class="layui-layer-content"]'), 'textContent')
        # 关闭窗口
        self.x_edit_project_name()

        return msg

    def x_edit_project_name(self):
        """关闭编辑项目名称窗口"""
        self.click((By.XPATH, "/html[1]/body[1]/div[5]/span[1]/a[1]"))

    def get_tr_of_current_stage(self, is_update_stage=False):
        """获取当前项目分期所在tr"""

        if is_update_stage:
            stage = config.get_cfg('project', 'stage_a')
        else:
            stage = config.get_cfg('project', 'stage')
        return self.find_element((By.XPATH, f'//td//div//span[text()="{stage}"]/../../..'))

    def get_tr_of_current_project(self):
        """获取当前项目所在tr"""

        project_name = config.get_cfg('project', 'project_name')
        return self.find_element((By.XPATH, f'//td//div//a[text()="{project_name}"]/../../..'))


class EditProjectBasicInfo(ProjectList):
    """编辑项目基本情况"""

    def developer_status(self, option=2):
        """
        开发商状况

        :param option: 下拉选项，默认2
            2：集团内企业
            3：为本项目专门成立的房地产开发企业
            4：专门房地产开发企业
            5：建筑企业派生房地产开发企业
            6：其他
        :type option: int
        :return: 开发商状况
        :rtype: str
        """
        locator = (By.XPATH, '//*[@id="editjbqk"]/div[1]/form/table/tbody/tr[1]/td[1]/div/div/input')
        self.click(locator)
        if option == 2:
            self.click((By.XPATH, '//*[@id="editjbqk"]/div[1]/form/table/tbody/tr[1]/td[1]/div/dl/dd[2]'))
        else:
            self.click((By.XPATH, '//*[@id="editjbqk"]/div[1]/form/table/tbody/tr[1]/td[1]/div/dl/dd[3]'))
        return self.get_attribute(locator, 'value')

    def project_nature(self, option=2):
        """
        项目性质

        :param option: 下拉选项，默认2
            1：空
            2: 正常的房地产开发项目
            3：危房（旧城）改造
            4：经济适用房、公租房等政府保障房
            5：其他政策性建房项目
            6：集资建房
        :type option: int
        :return: 项目性质
        :rtype: str
        """
        locator = (By.XPATH, '//tr[1]//td[2]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator, 'value')

    def form_of_land_acquisition(self, option=2):
        """
        土地取得形式

        :param option: 下拉选项，默认2
            1：空
            2：“招、拍、挂”取得
            3：改变原土地用途
            4：补交土地出让金
            5：其他
        :type option: int
        :return: 土地取得形式
        :rtype: str
        """

        locator = (By.XPATH, "//tr[2]//td[1]//div[1]//div[1]//input[1]")
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator, 'value')

    def land_status(self, option=2):
        """
        土地取得时地上状况

        :param option: 下拉选项，默认2
            1：空
            2：农村土地
            3：城镇空闲地
            4：工厂、企业用地
            5：居民住宅
            6：国土资源部已收储土地
        :type option: int
        :return: 土地取得时地上状况
        :rtype: str
        """

        locator = (By.XPATH, "//tr[2]//td[2]//div[1]//div[1]//input[1]")
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator, 'value')

    def development_situation_of_same_one(self, option=2):
        """
        同一地块开发状况

        :param option: 下拉选项，默认2
            1：一次开发
            2：两期开发
            3：多期开发
        :type option: int
        :return: 同一地块开发状况
        :rtype: str
        """
        locator = (By.XPATH, '//tr[3]//td[1]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator)

    def selection_mode_of_construction_unit(self, option=2):
        """
        建设单位选择方式

        :param option: 下拉选项，默认2
            1：空
            2：公开招标
            3：邀请招标
            4：房地产开发企业自建
            5：其他方式
        :type option: int
        :return: 建设单位选择方式
        :rtype: str
        """
        locator = (By.XPATH, '//tr[3]//td[2]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator)

    def centralized_construction_date_start(self, option=1):
        """
        建设日期起

        :param option: 选择日期，默认当前日期
            1: 当前日期
            2: 明天
        :type option: int
        :return: 建设日期起
        :rtype: str
        """
        locator = (By.ID, 'xmlbdate3')
        # self.click(locator)
        # time.sleep(1)
        # js = 'document.getElementById("xmlbdate3").removeAttribute("readonly")'
        # self.execute_script(js)
        self.remove_attribute(locator)
        self.select_date(locator, option)
        return self.get_attribute(locator)

    def centralized_construction_date_end(self, option=1):
        """
        建设日期止

        :param option: 选择日期，默认当前日期
            1: 当前日期
            2: 明天
        :type option: int
        :return: 建设日期止
        :rtype: str
        """
        locator = (By.ID, 'xmlbdate4')
        # self.click(locator)
        # time.sleep(1)
        # js = 'document.getElementById("xmlbdate4").removeAttribute("readonly")'
        # self.execute_script(js)
        self.remove_attribute(locator)
        self.select_date(locator, option)
        return self.get_attribute(locator)

    def basic_structure_form(self, option=2):
        """
        基础构造形式

        :param option: 下拉选项，默认2
            1：空
            2：条形基础
            3：独立基础
            4：满堂基础
            5：桩基础
        :type option: int
        :return: 基础构造形式
        :rtype: str
        """
        locator = (By.XPATH, '//tr[5]//td[1]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator)

    def elevator(self, option=2):
        """
        主要建筑物电梯配置

        :param option: 下拉选项，默认2
            1：空
            2：有
            3：无
        :type option: int
        :return: 主要建筑物电梯配置
        :rtype: str
        """
        locator = (By.XPATH, '//tr[5]//td[2]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator)

    def land_type(self, option=2):
        """
        项目所在区域土地

        :param option: 下拉选项，默认2
            1：空
            2：一级地段
            3：二级地段
            4：三级地段
            5：建制镇、工矿区
        :type option: int
        :return: 项目所在区域土地
        :rtype: str
        """
        locator = (By.XPATH, '//tr[6]//td[1]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator)

    def sales_way(self, option=2):
        """
        项目销售方式

        :param option: 下拉选项，默认2
            1：空
            2：自营销售
            3：消费者团购
            4：委托销售
            5：一次性买断
            6：售后返租
        :type option:
        :return: 项目销售方式
        :rtype: str
        """
        locator = (By.XPATH, '//tr[6]//td[2]//div[1]//div[1]//input[1]')
        self.click(locator)
        # time.sleep(1)
        self.click((By.XPATH, f"//div[@class='layui-form-select layui-form-selected']//dd[{option}]"))
        return self.get_attribute(locator)

    def centralized_sales_date_start(self, option=1):
        """
        销售日期起

        :param option: 日期选择
            1: 当前日期
            2: 18
        :type option: int
        :return: 销售日期起
        :rtype: str
        """
        locator = (By.ID, 'xmlbdate1')
        # self.click(locator)
        # time.sleep(1)
        # js = 'document.getElementById("xmlbdate1").removeAttribute("readonly")'
        # self.execute_script(js)
        self.remove_attribute(locator)
        self.select_date(locator, option)
        return self.get_attribute(locator)

    def centralized_sales_date_end(self, option=1):
        """
        销售日期止

        :param option: 日期选择
            1: 当前日期
            2: 16
            3: 18
        :type option: int
        :return: 销售日期止
        :rtype: str
        """
        locator = (By.ID, 'xmlbdate2')
        # self.click(locator)
        # time.sleep(1)
        # js = 'document.getElementById("xmlbdate2").removeAttribute("readonly")'
        # self.execute_script(js)
        self.remove_attribute(locator)
        self.select_date(locator, option)
        return self.get_attribute(locator)

    def transfer_for_free(self, checkbox=1):
        """
        项目包含的不能有偿转让的公共配套设施

        :param checkbox: 复选框，默认1
            1: 委员会、派出所用房
            2: 变电站、热力站、水厂用房
            3: 文体场馆、学校、幼儿园、托儿所用房
            4: 医院、邮电通信用房
            5: 人防工程
        :type checkbox: int
        :return: 项目包含的不能有偿转让的公共配套设施
        :rtype: str
        """
        self.click((By.XPATH,
                    f"/html[1]/body[1]/div[4]/div[2]/div[1]/div[1]/form[1]/table[1]/tbody[1]/tr[8]/td[1]/div[1]/div["
                    f"{checkbox}]/i[1]"))
        # 获取勾选的checkbox的value
        divs = self.find_elements((By.XPATH, "//div[@class='cbAll']//div"))
        res = []
        for div in divs:
            _class = div.get_attribute('class')
            if 'layui-form-checked' in _class.split(' '):
                text = div.find_element_by_tag_name('span').text

                inputs = self.find_elements((By.XPATH, "//div[@class='cbAll']//input"))
                for _input in inputs:
                    title = _input.get_attribute('title')
                    if title == text:
                        res.append(_input.get_attribute('value'))
        return '#'.join(res) + '#'

    def save_project_basic_info(self):
        """
        保存

        :return: 是否保存成功
        :rtype: str
        """

        # 点击保存按钮
        self.click((By.XPATH, "//a[@class='saveBlue rightHandel']"))
        locator = (By.CLASS_NAME, 'layui-layer-content')
        self.text_to_be_present_in_text(locator, '保存成功！')
        text = self.find_element(locator).text
        return text

    def project_basic_info_btn(self):
        """点击项目基本情况按钮"""

        tr = self.get_tr_of_current_stage()
        # self.click((By.XPATH, '//tr[2]//td[9]//div[1]//a[1]//span[1]'))
        tr.find_element_by_link_text('项目基本情况').click()

    def edit_project_basic_info(self, name, stage,
                                is_create_project=True,
                                developer_status=2,
                                project_nature=2,
                                form_of_land_acquisition=2,
                                land_status=2,
                                development_situation_of_same_one=2,
                                selection_mode_of_construction_unit=2,
                                construction_date_start=1,
                                construction_date_end=1,
                                basic_structure_form=2,
                                elevator=2,
                                land_type=2,
                                sales_way=2,
                                sales_date_start=1, sales_date_end=1,
                                checkbox=1):
        """
        编辑项目基本情况

        :param stage: 分期名称
        :type stage: str
        :param name: 项目名称
        :type name: str
        :param project_nature: 项目性质
        :type project_nature: int
        :param sales_way: 项目销售方式
        :type sales_way: int
        :param land_type: 项目所在区域土地
        :type land_type: int
        :param elevator: 主要建筑物电梯配置
        :type elevator: int
        :param basic_structure_form: 基础构造形式
        :type basic_structure_form: int
        :param selection_mode_of_construction_unit: 建设单位的选择方式
        :type selection_mode_of_construction_unit: int
        :param development_situation_of_same_one: 同一地块开发情况
        :type development_situation_of_same_one: int
        :param land_status: 土地取得时的地上状况
        :type land_status: int
        :param form_of_land_acquisition: 土地取得形式
        :type form_of_land_acquisition: int
        :param developer_status: 开发商状况
        :type developer_status: int
        :param is_create_project: 默认true
            false: 编辑企业基本情况，需要点击菜单
            true: 新建项目完成后自动弹出编辑页面，无需点击菜单
        :type is_create_project: bool
        :param construction_date_start: 建设期日期起
        :type construction_date_start: int
        :param construction_date_end: 建设期日期起止
        :type construction_date_end: int
        :param sales_date_start: 销售日期起
        :type sales_date_start: int
        :param sales_date_end: 销售日期止
        :type sales_date_end: int
        :param checkbox: 项目包含的不能有偿转让的公共配套设施数量
        :type checkbox: int
        :return: 保存相关信息
        :rtype: dict
        """

        # 如果是修改则需要点击菜单
        if not is_create_project:
            # 点击项目列表菜单
            self.click_project_list()
            self.project_basic_info_btn()
        # 之前获取提示信息时切换到了默认iframe，需要切换回到xmlb
        self.switch_to_xmlb()
        # 开发商状况
        developer_status = self.developer_status(developer_status)
        # 项目性质
        project_nature = self.project_nature(project_nature)
        # 土地取得形式
        form_of_land_acquisition = self.form_of_land_acquisition(form_of_land_acquisition)
        # 土地取得时的地上状况
        land_status = self.land_status(land_status)
        # 同一地块开发情况
        development_situation_of_same_one = self.development_situation_of_same_one(development_situation_of_same_one)
        # 建设单位的选择方式
        selection_mode_of_construction_unit = self.selection_mode_of_construction_unit(
            selection_mode_of_construction_unit)
        # 集中建设期间起
        construction_date_start = self.centralized_construction_date_start(construction_date_start)
        # 集中建设期间止
        construction_date_end = self.centralized_construction_date_end(construction_date_end)
        # 基础构造形式
        basic_structure_form = self.basic_structure_form(basic_structure_form)
        # 主要建筑物电梯配置
        elevator = self.elevator(elevator)
        # 项目所在区域土地
        land_type = self.land_type(land_type)
        # 项目销售方式
        sales_way = self.sales_way(sales_way)
        # 集中销售期起
        sales_date_start = self.centralized_sales_date_start(sales_date_start)
        # 集中销售期止
        sales_date_end = self.centralized_sales_date_end(sales_date_end)
        # 项目包含的不能有偿转让的公共配套设施
        checkbox = self.transfer_for_free(checkbox)
        input_info = [developer_status, project_nature, form_of_land_acquisition, land_status,
                      development_situation_of_same_one, selection_mode_of_construction_unit, construction_date_start,
                      construction_date_end, basic_structure_form, elevator, land_type, sales_way, sales_date_start,
                      sales_date_end, checkbox]

        # 保存
        res = self.save_project_basic_info()
        basic_info = get_project_basic_info(name, stage)
        if is_create_project and input_info == basic_info:
            self.logger.info('添加项目基本情况完成')
        elif not is_create_project and input_info == basic_info:
            self.logger.info('编辑项目基本情况完成')
        else:
            self.logger.error(f'页面数据：{input_info}')
            self.logger.error(f'表中数据：{basic_info}')
        self.driver.refresh()
        return {'msg': res, 'input_info': input_info, 'database_info': basic_info}


class CreateProject(ProjectList):
    """
    新建项目
    """

    logger = Logger('CreateProject').get_log()

    # 新建项目
    def click_create_project(self):
        self.click_project_list()
        self.click((By.XPATH, '/html[1]/body[1]/div[2]/div[1]/div[1]/div[2]/dl[1]/dt[1]/a[1]'))

    def input_project_name(self, name):
        """
        输入项目名称

        :param name: 项目名
        :type name: str
        :return: 项目名
        :rtype: str
        """

        locator = (By.NAME, 'XM_MC')
        self.send_keys(locator, name)
        return self.get_attribute(locator, 'value')

    def select_is_with_stage(self, is_with_stage):
        """
        有无分期

        :param is_with_stage: 有无分期
        :type is_with_stage: str
        :return: 有无分期
        :rtype: str
        """

        if is_with_stage == '有':
            num = 1
        else:
            num = 2

        # 有无分期单选框的xpath
        xpath = f'/html[1]/body[1]/div[3]/div[2]/div[1]/div[1]/dl[1]/dd[1]/form[1]/div[2]/div[1]/div[{num}]/i[1]'
        # 点击单选框
        self.click((By.XPATH, xpath))

        xpath_div = '/html[1]/body[1]/div[3]/div[2]/div[1]/div[1]/dl[1]/dd[1]/form[1]/div[2]'
        text = self.find_element((By.XPATH, xpath_div)).find_element(By.CLASS_NAME, 'layui-form-radioed').find_element(
            By.TAG_NAME, 'div').text
        return text

    def save_project(self, project_name, stage):
        """
        保存

        :return: 保存信息
        :rtype: dict
        """

        # 点击保存按钮
        xpath = '/html[1]/body[1]/div[3]/div[2]/div[1]/div[1]/div[2]/a[1]'  # 保存按钮xpath
        self.click((By.XPATH, xpath))
        self.switch_to_default_content()

        # 获取保存后的提示信息
        locator = (By.CLASS_NAME, 'layui-layer-content')  # 提示信息框
        # self.text_to_be_present_in_text(locator, '项目创建完成！')
        text = self.find_element(locator).text  # 获取提示信息

        # 写入xmid
        Base(self.driver).project_id = xmid = self.get_xmid(project_name, stage)
        config.set_cfg('project', 'project_id', xmid)
        # 设置分期名称
        Base(self.driver).stage = stage
        # 设置项目名称
        Base(self.driver).project_name = project_name
        is_insert = False
        if xmid:
            is_insert = True
        if xmid and text == '项目创建完成！':
            self.logger.info(f'项目创建完成，项目名称：{project_name}{stage}')
        else:
            self.logger.error('项目创建失败')
        return {'is_insert': is_insert, 'msg': text}

    def create_project(self, is_with_stage, is_cost_apportion, cost_apportion, is_land_apportion,
                       land_apportion, project_name, stage):
        """
        新建项目

        :param is_with_stage: 有无分期
        :type is_with_stage: str
        :param is_cost_apportion: 有无成本分摊，默认：有
        :type is_cost_apportion: str
        :param cost_apportion: 成本分摊比例，默认55.25
        :type cost_apportion: str
        :param is_land_apportion: 有无土地分摊，默认：有
        :type is_land_apportion: str
        :param land_apportion: 土地分摊比例，默认52.55
        :type land_apportion: str
        :param project_name: 项目名称
        :type project_name: str
        :param stage: 分期名称
        :type stage: str
        :return: 保存相关信息
        :rtype: dict
        """

        self.refresh()
        # 点击新建项目
        self.click_create_project()
        # 输入项目名称
        self.input_project_name(project_name)
        # 选择有无分期
        self.select_is_with_stage(is_with_stage)
        # 输入分期名称
        self.input_stage_name(stage)
        # 输入经纬度
        self.input_gnote()
        self.select_project_town()
        town = self.get_town()
        self.select_tax_organ(town)
        # 成本分摊比例
        if is_cost_apportion == '有':
            self.select_is_cost_apportion(is_cost_apportion)
            self.input_cost_apportion(cost_apportion)
        else:
            self.select_is_cost_apportion(is_cost_apportion)
        # 土地分摊比例
        if is_land_apportion == '有':
            self.select_is_land_apportion(is_land_apportion)
            self.input_land_apportion(land_apportion)
        else:
            self.select_is_land_apportion(is_land_apportion)
        return self.save_project(project_name=project_name, stage=stage)


class CreateStage(ProjectList):
    """
    新建分期
    """
    logger = Logger('CreateState').get_log()

    def create_stage(self, stage):
        """
        新建分期

        :param stage: 分期名称
        :type stage: str
        :return: 是否成功
        :rtype: str
        """

        self.refresh()
        self.click_project_list()
        # 点击新建分期
        tr = self.get_tr_of_current_project()
        tr.find_element_by_link_text('新建分期').click()
        # self.click((By.XPATH, '//tr[1]//td[11]//div[1]//a[1]//span[1]'))

        # 取得项目名称
        name = self.get_attribute((By.XPATH, "//input[@name='showname']"))
        # 输入分期
        self.input_stage_name(stage)
        # 输入经纬度
        self.input_gnote()
        # time.sleep(1)
        # 选择项目所属街道乡镇
        self.select_project_town()
        # 选择项目管理税务机关
        self.select_tax_organ()
        # 保存
        res = self.save_stage(name=name, stage=stage)
        return res

    def update_stage(self, stage):
        """
        编辑分期

        :return: 修改分期相关信息，包括：提示信息，页面数据，数据库数据
        :rtype: dict
        """

        self.refresh()
        self.click_project_list()
        # 编辑分期按钮
        tr = self.get_tr_of_current_stage(is_update_stage=True)
        tr.find_element_by_link_text('编辑').click()
        # self.click((By.XPATH, '//tr[3]//td[11]//div[1]//a[2]//span[1]'))
        # 取得项目名称
        name = self.get_attribute((By.XPATH, "//input[@name='showname']"))
        stage = self.input_stage_name(stage)
        pos = self.input_gnote(address='石家庄北站', no='114')
        gnote = pos.split(',')
        address = self.get_project_address()
        region = self.select_project_region(2)
        town = self.select_project_town(2)
        organ = self.select_tax_organ('建北街道')
        self.select_is_cost_apportion('有')
        self.input_cost_apportion()
        self.select_is_land_apportion('有')
        self.input_land_apportion()

        # 编辑附件
        # self.click((By.XPATH, "//a[@class='bdsc xmFiles']"))
        # # time.sleep(1)
        # self.switch_to_file_upload()
        # ProjectList.count = self.upload().get('count')
        # 保存
        self.click((By.XPATH, "//div[@id='slztid']//a[@class='saveBlue']"))
        # self.click((By.XPATH, "//a[@class='layui-layer-btn0']"))
        self.switch_to_default_content()
        text = self.get_attribute((By.XPATH, "//div[@class='layui-layer-content']"), 'textContent')
        info = [stage, address, region, town, gnote[1].strip(), gnote[0].strip(), organ]
        xmid = self.get_xmid(name, stage)
        info_database = get_stage_info(xmid)
        if info == info_database:
            self.logger.info('分期修改完成')
        else:
            self.logger.error('分期修改失败')
            self.logger.error(f'前台分期信息: {str(info)}')
            self.logger.error(f'后台分期信息: {str(info_database)}')
        return {'msg': text, 'info': info, 'info_database': info_database}

    # def check_files(self):
    #     """检查规划总平图上传附件与编辑分期中编辑附件中的附件是否一致"""
    #     self.click_file_upload_btn()
    #     self.switch_to_file_upload()
    #     count = self.get_count_files()
    #     self.logger.info('规划总平图附件个数: ' + str(count))
    #     self.logger.info('编辑分期中附件个数: ' + str(self.count))
    #     self.x_btn_upload()
    #     if count == self.count:
    #         return 'success'


def get_project_basic_info(name, stage):
    """
    获取项目基本情况
    """
    conn = Base.get_conn()
    cursor = conn.cursor()
    with conn:
        cursor.execute("""
                SELECT (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.kfszk) kfszk,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.xmxz) xmxz,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.tdqdxs) tdqdxs,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.tdqdsddsqk) tdqdsddsqk,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.tydkkfqk) tydkkfqk,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.jsdwsxzfs) jsdwsxzfs,
                   t.jzjsqjq,
                   t.jzjsqjz,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.jcgzxs) jcgzxs,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.zyjzwdtpz) zyjzwdtpz,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.xmszqytd) xmszqytd,
                   (SELECT name FROM dbo.zd_ComboxData WHERE Code = t.xmxsfs) xmxsfs,
                   t.jzxsqjq,
                   t.jzxsqjz,
                   t.bnyczrptss
                FROM dbo.Djxmdjfb t
                WHERE t.xm_id =
                (
                    SELECT d.XM_ID
                    FROM dbo.DJxmdj d
                    WHERE d.XM_MC = ?
                          AND d.Fqms = ?
                )
                """, (name, stage))
        res = cursor.fetchone()
    if not res:
        res = ''
    return list(res)


def get_stage_info(xmid):
    """获取分期信息"""
    conn = Base.get_conn()
    cursor = conn.cursor()
    with conn:
        cursor.execute("""
                                SELECT
                                    t.Fqms,
                                    t.Xmdz,
                                    a.Name,
                                    b.JDName,
                                    t.lat,
                                    t.lng,
                                    t.swjgMC 
                                FROM
                                    DJxmdj t
                                    INNER JOIN ZDXzqh a ON a.Code = t.SSXzqh
                                    INNER JOIN ZDJdxz b ON b.JDCode = t.SSJdxz 
                                WHERE
                                    t.XM_ID = ?
                """, xmid)
        return list(cursor.fetchone())
